package com.packet.cheng.controller;

import com.alibaba.fastjson.JSON;
import com.packet.cheng.dto.ConcatListParamDTO;
import com.packet.cheng.entity.ConcatList;
import com.packet.cheng.entity.User;
import com.packet.cheng.service.ConcatListService;
import com.packet.cheng.service.UserService;
import com.packet.cheng.util.R;
import com.packet.cheng.util.StringUtil;
import com.packet.cheng.vo.ConcatListVo;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author 张在成
 */
@RestController
@RequestMapping("/concatList")
public class ConcatListController {

    @Resource
    private UserService userService;

    @Resource
    private ConcatListService concatListService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private Redisson redisson;

    /**
     *  首先进行账号搜索，看该好友是否为红包平台用户
     *  添加好友功能(调用该接口去添加好友):
     * @param dto
     * @return
     * @throws Exception
     */
    @PostMapping("/addGoodFriend")
    public R<Boolean>  addGoodFriend(@RequestBody ConcatListParamDTO dto) throws Exception {
        R<Boolean> voR = concatListService.addGoodFriend(dto);
        return voR;
    }

    /**
     * 获取好友列表
     * @param token
     * @param dto
     * @return
     * @throws Exception
     */
    @PostMapping("/selectGoodFriendList")
    public R<List<ConcatList>>  selectGoodFriendList(@RequestHeader("token") String token,@RequestBody ConcatListParamDTO dto) throws Exception {
        if (!redisTemplate.hasKey(token)){
            return  R.data(400,"用户已过期,请重新登录",null);
        };
        //根据token获取登录用户信息
        User user = (User) redisTemplate.opsForValue().get(token);
        if (Objects.isNull(user)){
            return  R.data(400,"没有该用户,不能获取好友列表",null);
        }
        String string = JSON.toJSONString(user);
        System.out.println(string);
        R<List<ConcatList>> listR = concatListService.selectGoodFriendByUserId(dto);
        return  listR;
    }
}
